package com.bootdo.wx.api;

import cn.hutool.core.map.MapUtil;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.common.utils.UUIDUtils;
import com.bootdo.wx.domain.*;
import com.bootdo.wx.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HP on 2019/6/17.
 */
@Controller
@RequestMapping("/api/cms")
public class ApiCmsController {

    @Autowired
    private CmsSlideService cmsSlideService;
    @Autowired
    private GoodsSpikeService goodsSpikeService;
    @Autowired
    private GoodsNineService goodsNineService;
    @Autowired
    private GoodsBargainService bargainService;
    @Autowired
    private CmsNoticeSysService cmsNoticeSysService;
    @Autowired
    private GoodsGoodsService goodsGoodsService;
    @Autowired
    private CmsMaterialService cmsMaterialService;
    @Autowired
    private CmsWeJoinService cmsWeJoinService;
    @Autowired
    private CmsWeContactService cmsWeContactService;
    @Autowired
    private CmsCompanyService cmsCompanyService;
    @Autowired
    private CmsProblemService cmsProblemService;
    @Autowired
    private CmsProblemClassService cmsProblemClassService;


    /***
     * 根据分类id 获取问题名字
     * @param params
     * @return
     */
    @PostMapping( "/getProblemNameByClassId")
    @ResponseBody
    public R getProblemNameByClassId(@RequestBody Map<String, Object> params){

        if (!params.containsKey("classId")|| StringUtils.isBlank(params.get("classId").toString())
        ){
            return R.error("参数错误");
        }

        try{
            return cmsProblemService.getProblemNameByClassId(params);


        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 问题分类
     * @return
     */
    @PostMapping( "/getProblemClass")
    @ResponseBody
    public R getProblemClass(){


        try{
            HashMap<String, Object> objectObjectHashMap = MapUtil.newHashMap();
            objectObjectHashMap.put("delFlag","0");
            objectObjectHashMap.put("sort","location");
            objectObjectHashMap.put("order","asc");
            List<CmsProblemClassDO> list = cmsProblemClassService.list(objectObjectHashMap);
            return R.ok().put("data",list);

        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
    
    
    /***
     * 
     * @author long
     * 2020年8月6日上午11:38:45
     * 方法释义:常见问题详情
     * 参数:
     *
     */
    @PostMapping( "/getProblemInfo")
    @ResponseBody
    public R getProblemInfo(@RequestBody Map<String, Object> params){
    	 if (!params.containsKey("id")|| StringUtils.isBlank(params.get("id").toString())
    	            ){
    	            return R.error("参数错误");
    	        }
        try{
        	return cmsProblemService.getProblemInfo(params);
           
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
    
    /***
     * 
     * @author long
     * 2020年8月6日上午11:38:45
     * 方法释义:常见问题列表
     * 参数:
     *
     */
    @PostMapping( "/getProblemList")
    @ResponseBody
    public R getProblemList(@RequestBody Map<String, Object> params){
        try{
        	return cmsProblemService.getProblemList(params);
           
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
    
    
    /**
     * 
     * @author long
     * 2020年7月17日下午5:03:48
     * 方法释义:公司介绍
     * 参数:
     *
     */
    @PostMapping( "/getCompany")
    @ResponseBody
    public R getCompany(){
        try{
        	Map<String, Object> map = new HashMap<>();
        	map.put("delFlag", "0");
        	List<CmsCompanyDO> list = cmsCompanyService.list(map);
        	if(list.size()>0){
        		 return R.ok().put("data", list.get(0));
        	}else{
        		return R.error("没有对应内容");
        	}
           
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
    
    /**
     * 
     * @author long
     * 2020年7月17日下午5:03:48
     * 方法释义:联系我们
     * 参数:
     *
     */
    @PostMapping( "/contactWe")
    @ResponseBody
    public R contactWe(){
        try{
        	Map<String, Object> map = new HashMap<>();
        	map.put("delFlag", "0");
        	List<CmsWeContactDO> likeList = cmsWeContactService.list(map);
            return R.ok().put("data", likeList);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
    
    /**
     * 
     * @author long
     * 2020年7月17日下午5:03:48
     * 方法释义:加入我们
     * 参数:
     *
     */
    @PostMapping( "/addWe")
    @ResponseBody
    public R addWe(@RequestBody Map<String, Object> params){
        if (!params.containsKey("phone")|| StringUtils.isBlank(params.get("phone").toString())
        	||!params.containsKey("name")|| StringUtils.isBlank(params.get("name").toString())	
            ){
            return R.error("参数错误");
        }
        try{
        	params.put("delFlag", "0");
        	List<CmsWeJoinDO> list = cmsWeJoinService.list(params);
        	if(list.size()>0){
        		return R.error("您已填写过信息,请勿重复添加");
        	}
        	CmsWeJoinDO cmsWeJoinDO = new CmsWeJoinDO();
        	BeanUtils.populate(cmsWeJoinDO, params);
        	cmsWeJoinDO.setCaAt(new Date());
        	cmsWeJoinDO.setDelFlag(0);
        	cmsWeJoinDO.setId(UUIDUtils.getUUID());
            cmsWeJoinService.save(cmsWeJoinDO);
            return R.ok().put("data", cmsWeJoinDO);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 首页轮播图列表
     * 图片类型：0：首页轮播图，1：支付宝支付二维码
     */
    @PostMapping( "/slide/list")
    @ResponseBody
    public R slideList(@RequestBody Map<String, Object> params){
        if (!params.containsKey("type")|| StringUtils.isBlank(params.get("type").toString())
            ){
            return R.error("参数错误");
        }
        try{
            List<CmsSlideDO> list = cmsSlideService.queryAll(params);
            return R.ok().put("data",list);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 首页9.9专区展示
     */
    /*@GetMapping( "/nine")
    @ResponseBody
    public R nine(){
        try{
            Map<String,Object> map = goodsNineService.nine();
            return R.ok().put("data",map);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }*/


    /*
   * 首页砍价专区展示
   * */
    /*@ResponseBody
    @GetMapping("/bargain")
    public R bargain() {
        try{
            Map<String,Object> map = bargainService.bargain();
            return R.ok().put("data", map);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("查询失败");
        }
    }*/

    /*
   * 我的消息（公告）列表
   * */
    @PostMapping( "/noticeSys/list")
    @ResponseBody
    public R noticeSysList(@RequestBody Map<String, Object> params){
        if (!params.containsKey("pageNumber")||!params.containsKey("pageSize")){
            return R.error("参数异常");
        }
        try{
            PageInfo<CmsNoticeSysDO> list = cmsNoticeSysService.queryAll(params);
            return R.ok().put("data",list);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /**
     * 系统公告详情
     */
    @PostMapping( "/noticeSys/info")
    @ResponseBody
    public R noticeSysInfo(@RequestBody Map<String, Object> params){
        if (!params.containsKey("noticeSysId")){
            return R.error("参数异常");
        }
        try{
            String noticeSysId = params.get("noticeSysId").toString();
            CmsNoticeSysDO list = cmsNoticeSysService.get(noticeSysId);
            return R.ok().put("data",list);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }


    /**
     * 首页商品专区展示
     */
    /*@GetMapping( "/nine")
    @ResponseBody
    public R nine(){
        try{
            Map<String,Object> params = new HashMap<String,Object>();
            Map<String,Object> map = goodsGoodsService.getCmsGoods(params);
            return R.ok().put("data",map);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }*/


    /**
     * 官方素材列表
     */
    @PostMapping( "/material/list")
    @ResponseBody
    public R materialList(@RequestBody Map<String, Object> params){
        if (!params.containsKey("pageNumber")||!params.containsKey("pageSize")){
            return R.error("参数异常");
        }
        try{
            PageInfo<CmsMaterialDO> list = cmsMaterialService.queryAll(params);
            return R.ok().put("data",list);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }








}
